'use strict';
import React, {Component} from 'react';
import {
    Alert,
    Platform,
} from 'react-native';
import * as types from '../constants/action-types';

import Http from '../core/utils/http';
import API from '../constants/server-apis';
import InitDB from '../core/database/init-db';

//用户登陆
export function login(userName, userPwd) {
    return dispatch => {
        const params = {
            username: userName,
            password: userPwd,
            empId:userName,
        }

        dispatch(fetchLogin());
        return Http.fetch(API.API_USER_LOGIN_URL, params).then(json=>{

            if(json.returnType.resultCode==1){
                //登录成功
                dispatch(receiveLogin({userInfo:json}))
            }
            return json;
        })
    }
}

function fetchLogin() {
    return {
        type: types.FETCH_LOGIN,
    };
}

function receiveLogin(userInfo) {
    return {
        type: types.RECEIVE_LOGIN,
        userInfo: userInfo,
    };
}


//修改用户密码
export function changePassword(oldPassword, newPassword) {
    return dispatch => {
        const params = {
            oldPassword: oldPassword,
            newPassword: newPassword,
        }

        dispatch(fetchChangePwd());
        return Http.fetch(API.API_USER_CHANGE_PWD_URL, params).then(json=>{
            if(json.returnType.resultCode==1){
                //登录成功
                dispatch(receiveChangePwd())
            }
            return json;
        })
    }
}

function fetchChangePwd() {
    return {
        type: types.FETCH_CHANGE_PWD,
    };
}

function receiveChangePwd() {
    return {
        type: types.RECEIVE_CHANGE_PWD,
    };
}



/**
 * 获取用户的技能
 * @param userSkillId
 * @returns {function(*)}
 */
export function getSkillDetail(userSkillId) {
    return dispatch => {
        const params = Object.assign({
            objId:'userSkillDetail',
            user_skill_id:userSkillId,
        });


        return Http.fetch(API.API_USER_SKILL_DETAIL_URL, params).then(json=>{
            console.log(json,"------>getSkillDetail")
            if(json && json.hasOwnProperty('userSkillDetail') && json.userSkillDetail.length>=1){
                dispatch(receiveSkillDetail(userSkillId,json.userSkillDetail[0]))
            }
            return json;
        })
    }
}

function fetchSkillDetail() {
    return {
        type: types.FETCH_SKILL_DETAIL,
    };
}

function receiveSkillDetail(userSkillId,skillInfo) {
    return {
        type: types.RECEIVE_SKILL_DETAIL,
        userSkillId:userSkillId,
        skillInfo:skillInfo,
    };
}

/**
 * 关注用户
 * @param personId
 * @param isFocus
 * @returns {function(*)}
 */
export function focus(personId,isFocus) {
    return dispatch => {
        const params = Object.assign({
            person_id:personId,
            state:isFocus?"1":"0",
        });
        
        return Http.fetch(API.API_USER_FOCUS_URL, params).then(json=>{
            console.log(json,'focus-----receive')
            return json;
        })
    }
}

function fetchFocus() {
    return {
        type: types.FETCH_SKILL_DETAIL,
    };
}

function receiveFocus() {
    return {
        type: types.RECEIVE_SKILL_DETAIL,
    };
}


export function getUserDetail(personId) {
    return dispatch => {
        const params = Object.assign({
            objId:'userDetail',
            person_id:personId,
        });

        return Http.fetch(API.API_USER_DETAIL_URL, params).then(json=>{
            console.log(json,"------>getUserDetail")
            if(json && json.hasOwnProperty('userDetail') && json.userDetail.length>=1){
                dispatch(receiveUserDetail(personId,json.userDetail[0]))
            }
            return json;
        })
    }
}

function fetchUserDetail() {
    return {
        type: types.FETCH_USER_DETAIL,
    };
}

function receiveUserDetail(userId,userDetail) {
    return {
        type: types.RECEIVE_USER_DETAIL,
        userId:userId,
        userDetail:userDetail,
    };
}





